<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdminConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdminConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdminConfig(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('key');
            $grid->column("name")->limit(50, '...');
            $grid->column("image")->image('', 50, 50);
            $grid->column("content")->limit(50, '...');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdminConfig(), function (Show $show) {
            $show->field('id');
            $show->field('key');
            $show->field("image")->image();
            $show->field("name");
            $show->field("content");
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminConfig(), function (Form $form) {
            $form->text('key')->required();
            $form->checkbox('checkbox', '多选')
                ->options([
                    1 => '显示图片',
                    2 => '显示文本框',
                    3 => '显示长文本',
                    //4 => '显示编辑器'
                ])->when(1, function (Form $form) {
                    $form->image('image')
                        ->move('/public/admin_config/' . date('Ym'))
                        ->autoUpload()
                        ->removable()
                        ->uniqueName()
                        ->saveAsString();
                })->when(2, function (Form $form) {
                    $form->text('name')->saveAsString();
                })->when(3, function (Form $form) {
                    $form->textarea('content')->saveAsString();
                })->when(4, function (Form $form) {
                    //$form->editor('content', '内容');
                })->saving(function ($v) {
                    // 转为 , 隔开的字符串
                    return implode(',', $v);
                });
        });
    }
}
